package org.six11.ss.client;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.six11.ss.shared.Message;
import org.six11.util.adt.MultiState;

/**
 * 
 *
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class LabelsMessageHandler extends AWTMessageHandler {

  /**
   * @param state
   */
  public LabelsMessageHandler(MultiState state) {
    super(state);
  }

  /**
   * 
   */
  @Override
  public void handleAWT(Message msg) {
    List<LabelInfo> labels = new ArrayList<LabelInfo>();
    List elmObjects = msg.getRoot().getChildren("label");
    bug("Received labels from the server. I found " + elmObjects.size() + " of them.");
    bug("Here is the exact message:\n" + msg.toXml());
    for (Object obj : elmObjects) {
      Element elm = (Element) obj;
      LabelInfo label = new LabelInfo();
      label.setLabelerName(elm.getAttributeValue("labeler"));
      label.setText(elm.getAttributeValue("text"));
      label.setTime(Long.parseLong(elm.getAttributeValue("serverTime")));
      labels.add(label);
    }
    state.setValue(ClientStateNames.K_LABELS, labels);
  }

}
